package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.OverflowListDao;
import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.OverflowListGoodsService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {

    @Autowired
    private OverflowListGoodsDao overflowListGoodsDao;

    @Autowired
    private OverflowListDao overflowListDao;

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private UserDao userDao;

    @Override
    @Transactional
    public ServiceVO save(OverflowList overflowList, String overflowListGoodsStr,String overflowNumber) {

        OverflowList overflowListVo = new OverflowList();
        overflowListVo.setUserId(1);//不知道怎么获取直接给了，不过只有userId=1的那个才是管理员
        overflowListVo.setOverflowNumber(overflowNumber);//自己生成一个？？？？
        overflowListVo.setOverflowDate(overflowList.getOverflowDate());
        overflowListVo.setRemarks(overflowList.getRemarks());
        overflowListVo.setTrueName(overflowList.getTrueName());
        overflowListDao.save(overflowListVo);


        Gson gson = new Gson();
        //类型要转换成list
        OverflowListGoods[] overflowListGoods = gson.fromJson(overflowListGoodsStr, OverflowListGoods[].class);
        for (OverflowListGoods overflowListGood : overflowListGoods) {
            OverflowListGoods overflowListGoodsVo = new OverflowListGoods();
            overflowListGoodsVo.setOverflowListId(overflowListVo.getOverflowListId());
            overflowListGoodsVo.setGoodsId(overflowListGood.getGoodsId());
            overflowListGoodsVo.setGoodsCode(overflowListGood.getGoodsCode());
            overflowListGoodsVo.setGoodsName(overflowListGood.getGoodsName());
            overflowListGoodsVo.setGoodsModel(overflowListGood.getGoodsModel());
            overflowListGoodsVo.setGoodsUnit(overflowListGood.getGoodsUnit());
            overflowListGoodsVo.setGoodsNum(overflowListGood.getGoodsNum());
            overflowListGoodsVo.setPrice(overflowListGood.getPrice());
            overflowListGoodsVo.setTotal(overflowListGood.getTotal());
            overflowListGoodsVo.setOverflowListId(overflowListVo.getOverflowListId());
            overflowListGoodsVo.setGoodsTypeId(overflowListGood.getGoodsId());
            overflowListGoodsDao.save(overflowListGoodsVo);
        }

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public List<OverflowList> selectByTime(String sTime, String eTime) {
        List<OverflowList> lists=overflowListDao.selectByTime(sTime,eTime);
        for (OverflowList list : lists) {
            User userById = userDao.getUserById(list.getUserId());
            list.setTrueName(userById.getTrueName());
        }
        return lists;
    }

    @Override
    public List<OverflowListGoods> goodsList(Integer overflowListId) {
        List<OverflowListGoods> listGoods=overflowListGoodsDao.goodsList(overflowListId);
        return listGoods;
    }
}
